<?php

namespace frontend\modules\wechat\controllers;

use common\models\wechat\WechatAccounts;
use common\models\wechat\WechatFans;
use EasyWeChat\Message\Image;
use EasyWeChat\Message\Music;
use EasyWeChat\Message\News;
use EasyWeChat\Message\Text;
use EasyWeChat\Message\Video;
use EasyWeChat\Message\Voice;
use frontend\controllers\BaseController;

class ProcessController extends BaseController
{

    /**
     * 微信请求的消息内容
     * @var array
     */
    public $message;
    /**
     * 微信请求关闭CSRF验证
     * @var bool
     */
    public $enableCsrfValidation = false;
    /**
     * @var Wechat;
     */
    private $_wechat;



    /**
     * 设置公众号
     * @param Wechat $wechat
     */
    public function setWechat(WechatAccounts $wechat)
    {
        $this->_wechat = $wechat;
    }

    /**
     * @return mixed
     * @throws InvalidConfigException
     */
    public function getWechat()
    {
        if ($this->_wechat === null) {
            throw new InvalidConfigException('The "wechat" property must be set.');
        }
        return $this->_wechat;
    }


    private $_fans = false;
    /**
     * 获取触发微信请求的微信用户信息
     * @return Fans
     */
    public function getFans()
    {
        if ($this->_fans === false) {
            \Yii::warning('获取粉丝信息1');
            $this->_fans = WechatFans::findByOpenId($this->message['FromUserName']);
        }
        \Yii::warning('获取粉丝信息2');
        return $this->_fans;
    }

    /**
     * 响应文本消息
     * 例: $this->responseText('hello world');
     * @param $content
     * @return array
     */
    public function responseText($content)
    {
        $server = \Yii::$app->wechat->server;
        $server->setMessageHandler(function ($message) use($content) {
            return $content;
        });
        $response = $server->serve();
        $response->send();

    }

    /**
     * 响应图文消息
     * @param array $articles
     * @return array
     */
    public function responseNews(array $articles)
    {
        if (isset($articles['title'])) {
            $news = [$articles];
        }else{
            foreach ($articles as $article) {
                $news[] = new News([
                    'title'       => $article['title'],
                    'description' => $article['description'],
                    'url'         => $article['url'],
                    'image'       => $article['thumb']
                ]);
            }
        }

        $server = \Yii::$app->wechat->server;
        $server->setMessageHandler(function ($message) use($news) {
            return $news;
        });
        $response = $server->serve();
        $response->send();
    }

    /**
     * 响应图片消息
     * @param $mid 图片mid(需先上传图片给wechat服务器获得mid)
     * @return array
     */
    public function responseImage($mediaId)
    {
        $server = \Yii::$app->wechat->server;
        $server->setMessageHandler(function ($message) use($mediaId) {
            return new Image(['media_id'=>$mediaId]);
        });
        $response = $server->serve();
        $response->send();
    }

    /**
     * 响应语音消息
     * @param $mid 语音mid(需先上传语音给wechat服务器获得mid)
     * @return array
     */
    public function responseVoice($mediaId)
    {
        $server = \Yii::$app->wechat->server;
        $server->setMessageHandler(function ($message) use($mediaId) {
            return new Voice(['media_id'=>$mediaId]);
        });
        $response = $server->serve();
        $response->send();
    }

    /**
     * 响应视频消息
     * @param array $video mid(需先上传视频给wechat服务器获得mid和thumbMid)
     * @return array
     */
    public function responseVideo(array $video)
    {
        $server = \Yii::$app->wechat->server;
        $server->setMessageHandler(function ($message) use($video) {
            return new Video([
                'title' => $video['title'],
                'description' => $video['description'],
                'media_id' => $video['mediaid']
            ]);
        });
        $response = $server->serve();
        $response->send();

    }

    /**
     * 响应音乐消息
     * @param array $music
     * @return array
     */
    public function responseMusic(array $music)
    {
        $server = \Yii::$app->wechat->server;
        \Yii::$app->wechat->server->setMessageHandler(function ($message) use($music) {
            return new Music([
                'title' => $music['title'],
                'description' => $music['description'],
                'url' => $music['url'],
                'hq_url' => $music['hq_url']
            ]);
        });
        $response = $server->serve();
        $response->send();
    }

}
